package com.kyw.android_dev_util.service;

import com.kyw.android_dev_util.res.pc.FileBean;
import com.kyw.android_dev_util.util.FileUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class FileService {
    public List<FileBean> getFile(String path, boolean searchChildDir) {
        // get file list where the path has
        File file = new File(path);
        // get the folder list
        File[] array = file.listFiles();

        List<FileBean> files = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            if (array[i].isFile()) {
                FileBean fileBean = new FileBean();
                fileBean.fileName = array[i].getName();
                fileBean.filePath = array[i].getPath();
                files.add(fileBean);
            } else if (array[i].isDirectory()) {
                if(searchChildDir){
                    getFile(array[i].getPath(),searchChildDir);
                }

            }
        }
        return files;
    }
    public void updateIdToCode(String layoutPath,String codePath) throws IOException {
        Map<String, String> ids = FileUtil.findIds(layoutPath);
        String content = FileUtil.createIdInCode(codePath, ids);
        FileUtil.writeToFile(codePath,content);
    }
}
